/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unp.tasi.loja.entidades;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
 *
 * @author jfrank
 */
@Entity
@Table(name = "pedido")
public class Pedido implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  @ManyToOne
  private Usuario usuario;
  @OneToMany(mappedBy = "pedido", cascade = CascadeType.ALL)
  private List<ItemPedido> itens;
  @Enumerated(EnumType.ORDINAL)
  private StatusPedido status;

  public Pedido() {
    this.status = StatusPedido.ABERTO;
    this.itens = new ArrayList();
  }

  public Double getTotal() {
    return getTotalCentavos() / 100.0;
  }

  public Long getTotalCentavos() {
    Long total = 0L;
    for (ItemPedido item : itens) {
      total += item.getTotalCentavos();
    }
    return total;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Pedido)) {
      return false;
    }
    Pedido other = (Pedido) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "br.unp.tasi.loja.entidades.Pedido[id=" + id + "]";
  }

  /**
   * @return the usuario
   */
  public Usuario getUsuario() {
    return usuario;
  }

  /**
   * @param usuario the usuario to set
   */
  public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
  }

  /**
   * @return the itens
   */
  public List<ItemPedido> getItens() {
    return itens;
  }

  /**
   * @param itens the itens to set
   */
  public void setItens(List<ItemPedido> itens) {
    this.itens = itens;
  }

  /**
   * @return the status
   */
  public StatusPedido getStatus() {
    return status;
  }

  /**
   * @param status the status to set
   */
  public void setStatus(StatusPedido status) {
    this.status = status;
  }
}
